﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Reflection;
using System.Resources;
using System.Security.AccessControl;
using System.Text;
using System.Windows.Media.Imaging;
using Model;

namespace DataAccess
{
    public static class PhotoAlbumDataAccess
    {
        public static void InsertImageToComplaint(long complaintId, string filePath, string fileName)
        {
            String assemblyPath = Assembly.GetExecutingAssembly().Location;
            int lI = assemblyPath.LastIndexOf(@"\");
            assemblyPath = assemblyPath.Remove(lI, assemblyPath.Length - lI);
            string path = Path.GetFullPath(assemblyPath);
            if (!Directory.Exists(Path.GetFullPath(path+@"\Photos\") + complaintId))
            {
                Directory.CreateDirectory(Path.GetFullPath(path+@"\Photos\") + complaintId);
            }
            File.Copy(filePath, Path.GetFullPath(path+@"\Photos\") + complaintId + @"\" + fileName);
        }

        public static String GetActorImagePath(long complaintId, string actorType)
        {
            var images = GetAllImagesData(complaintId);
            foreach (var imageEntity in images)
            {
                if (imageEntity.Name == actorType + ".jpg")
                    return imageEntity.ImagePath;
            }
            String assemblyPath = Assembly.GetExecutingAssembly().Location;
            int lI = assemblyPath.LastIndexOf(@"\");
            assemblyPath = assemblyPath.Remove(lI, assemblyPath.Length - lI);
            string path = Path.GetFullPath(assemblyPath + @"/NoImage.jpg");
            CheckNoImage();
            return path;
        }

        private static void CheckNoImage()
        {
            String assemblyPath = Assembly.GetExecutingAssembly().Location;
            int lI = assemblyPath.LastIndexOf(@"\");
            assemblyPath = assemblyPath.Remove(lI, assemblyPath.Length - lI);
            string path = Path.GetFullPath(assemblyPath + @"/NoImage.jpg");
            if (!File.Exists(path))
            {
                ResourceManager rm = new ResourceManager("DataAccess.Properties.Resources",
                                                         Assembly.GetExecutingAssembly());
                System.Drawing.Bitmap defaultImage = (System.Drawing.Bitmap) rm.GetObject("NoImage");
                Bitmap bm = new Bitmap(defaultImage);
                defaultImage.Dispose();
                bm.Save(path);
                bm.Dispose();
            }
        }

        public static List<ImageEntity> GetAllImagesData(long complaintId)
        {
            List<ImageEntity> allImages = new List<ImageEntity>();
            try
            {
                String assemblyPath = Assembly.GetExecutingAssembly().Location;
                int lI = assemblyPath.LastIndexOf(@"\");
                assemblyPath = assemblyPath.Remove(lI, assemblyPath.Length - lI);
                string path = Path.GetFullPath(assemblyPath);
                string[] filePaths = Directory.GetFiles(path+@"\Photos\" + complaintId + @"\", "*.jpg");
                for (int i = 0; i < filePaths.Length; i++)
                {
                    var newEnt = new ImageEntity();
                    newEnt.ImagePath = Path.GetFullPath(filePaths[i]);
                    newEnt.Name = filePaths[i].Remove(0, (path+@"\Photos\" + complaintId + @"\").Length);
                    allImages.Add(newEnt);
                }
                if (filePaths.Length == 0)
                    throw new DirectoryNotFoundException();
            }
            catch (Exception)
            {
                
            }
            return allImages;
        }
    }
}
